import java.util.Arrays;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yang
 * Date: 2022-07-23
 * Time: 16:12
 */
public class HashBuck {

    static class Node {
        public int key;
        public int val;
        public Node next;

        public Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }

    public Node[] array;
    public int usedSize;//记录 当前哈希桶当中 有效数据的个数

    public HashBuck() {
        this.array = new Node[10];
        this.usedSize = 0;
    }

    /**
     * 存储key val
     *
     * @param key
     * @param val
     * @return
     */
    public void put(int key, int val) {
        Node node = new Node(key, val);
        int index = key%array.length;
        Node cur = array[index];
        if(cur == null){
            array[index] = node;
            return;
        }
        while(cur.next != null){
            if(cur.key == key){
                cur.val = val;
                return;
            }
            else cur = cur.next;
        }
        node.next = array[index];
        array[index] = node;
        usedSize++;
    }

    void grow(){
        Node[] newArray = new Node[array.length*2];
        for (int i = 0; i < array.length; i++) {
            for (Node node = array[i]; node != null ; node = node.next) {
                int index = node.key%newArray.length;
                Node newNode = new Node(node.key,node.val);
                newNode.next = array[index];
                array[index] = node;
            }
        }

        array = newArray;
    }

    /**
     * 通过key值 获取val 值
     *
     * @param key
     * @return
     */
    public int get(int key) {
        int index = key%array.length;
        Node cur = array[index];
        while(cur != null){
            if(cur.key == key){
                return cur.val;
            }
            else cur = cur.next;
        }
        return -1;
    }
}